<?php
class Merchant_Model_ProductStocket
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_ProductStocket();
	}
	
	public static function getInstance(){
		return new Merchant_Model_ProductStocket();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet $row product_stocket's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row product_stocket's data 
	*@param int $product_id 
	*@return boolean
	*/
	public  function update($row,$id,$warehouseId=1){
		$row['ps_last_update'] = date("Y-m-d H:i:s");
		$where = $this->getAdapter()->quoteInto('product_id = ?', $id);
		$where .= $this->getAdapter()->quoteInto(' and warehouse_id = ?', $warehouseId);
		
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $product_id 
	*@return boolean
	*/
	 public function delete($id,$warehouseId=1){
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id); 
		$where .= $this->_table->getAdapter()->quoteInto(' and warehouse_id = ?', $warehouseId);
		return $this->_table->delete($where);
	}
	
	public function getByProductId($id,$warehouseId=1){	
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id); 
		$where .= $this->_table->getAdapter()->quoteInto(' and warehouse_id = ?', $warehouseId);
		
		if($rowset = $this->_table->fetchRow($where)){			
			return $rowset->toArray();
		}	
		return false;			
	}
	
    public function getByCondition($condition=array(),$type='*',$orderby="product_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
		if(isset($condition["problem_product"]) && $condition["problem_product"]!=''){
            $select->where(" ps_quantity_problem > ?",0);
        }
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
// 			echo $sql;exit;
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}	
	
	public function getByConditionJoin($condition,$type='*',$orderby="",$page=0,$pageSize=0){
		$select = $this->_table->getAdapter()->select();
		$table  = $this->_table->info('name');
		
		$select->from($table,$type);
		$select->join('product', "{$table}.product_id = product.product_id", '*');
		$select->where("1 =?",1);
		
		if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
			$select->where(" product.product_sku =? ",$condition["product_sku"]);
		}
		
		if(isset($condition["product_title"]) && $condition["product_title"]!=''){
			$select->where(" product.product_title =? ",$condition["product_title"]);
		}
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
			$select->where(" product.customer_id = ? ",$condition["customer_id"]);
		}
		
		if(isset($condition["product_status"]) && $condition["product_status"]!=''){
			$select->where(" product.product_status = ?",$condition["product_status"]);
		}
		
		if(isset($condition["quantity_problem"]) && $condition["quantity_problem"]!=''){
			$select->where(" ps_quantity_problem > ?",0);
		}
		
		// 			print_r($select->__toString());exit;
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchone($select);
			
		}else{
			if($orderby!=''){
				$orderbyArr= explode(',',$orderby);
				foreach($orderbyArr as $order){
					$select->order($order);
				}
	
			}
			if($pageSize>0 && $page>0){
				$start = ($page-1)*$pageSize;
				
				$select->limit($pageSize,$start);
			}
			return $this->_table->getAdapter()->fetchAll($select);
		}
	
	}
	
	
	public function lockForUpdate($productId,$warehouseId=1){		 
		$sql = "select * from product_stocket where product_id='$productId' for update";
		$where .= $this->_table->getAdapter()->quoteInto(' and warehouse_id = ?', $warehouseId);
		return $this->_table->getAdapter()->fetchRow($sql);
	}
}